<?php
# 推送配送完成订单 --- POS
# POS 单号
# 物流名称
# 物流费用

$serverFilePath =realpath(dirname(__FILE__)); ;//realpath(dirname(__FILE__)); //$_SERVER["SCRIPT_FILENAME"];
$doc_dir = strrpos($serverFilePath, ''.DIRECTORY_SEPARATOR.'crontab');
$doc_dir = substr($serverFilePath, 0, $doc_dir + 1);   //project root
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'lib/Config.php';
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.'HybrisClass.php';
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.'Logging.php';
include_once $doc_dir . ''.DIRECTORY_SEPARATOR.'plugin'.DIRECTORY_SEPARATOR.'POSClass.php';

set_time_limit(0);
error_reporting(E_ALL);
header("Content-type:text/html;charset:utf-8");

$log = new Logging();
$log->lfile('pos_fee');
$config_ini = $doc_dir . 'config/config.ini';

$ini_array = parse_ini_file($config_ini, true);
$db_array = $ini_array['DB'];
$getWebsite = $ini_array['WebSite'];
$con = mysqli_init();

mysqli_real_connect($con, $db_array['host'], $db_array['id'], $db_array['pwd'], $db_array['database'], $db_array['port']);
mysqli_set_charset($con, $db_array['encoding']);
//$ph_no = "AND a.ph_no = 'POSX1001972G2012240913'";
$sql = " SELECT a.id,a.ph_no,a.pos_no,a.pos_nu,b.express_type,b.express_fee FROM orders a left join orders_express b  ON a.ph_no = b.ph_no where  a.status = 5 AND b.status = 5  AND a.isdeleted = 0  AND b.isdeleted = 0 AND a.pos_nu < 3 AND a.`source` = 'POS' ";
$result = mysqli_query($con, $sql);
//echo $sql;
$res = array();
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    $res[] = $row;
}
if($res && count($res) > 0){
    foreach ($res as $k => $val) {
        $Pos = new POSClass($getWebsite['debug']);
        $res = $Pos->PushFee($val);
        $log->lwrite ('发送数据：'.json_encode($val, JSON_UNESCAPED_UNICODE));
        $log->lwrite ('接收返回：'.json_encode($res, JSON_UNESCAPED_UNICODE));
        if($res && $res['status'] == 'success'){
            $columns = "pos_nu = 4";
        }else{
            $columns = "pos_nu = ".($val['pos_nu'] + 1);
        }
        $sql_edit = " update orders set $columns where id = ".$val['id'];
        $result = mysqli_query($con, $sql_edit);
        $log->lwrite ('修改数据：'.$sql_edit);
    }
}else
    echo '无订单可推送';
